$(function() {
	
	actEvent = {
        init : function () {
        	
			  this.lineCheckboxEvent();
			  this.lineButtonEvent();
			  this.lineTitleEvent();
			  this.delEvent();
			  this.setTopEvent();
			  this.cancelTopEvent();
			  this.checkAllEvent();
			  this.addInfoEvent();
			  this.sortEvent();
        },
        
        /* 行数据 checkbox 事件bind*/
		addInfoEvent : function (){
			$("#right01 .agen_right_form .agen_right_button .agen_add:eq('0')").click(function(){
				//alert($(this).parent('td').parent('tr').index());
				//alert($(this).val());
				$.ajax({
	    			  type: "POST",
	    			  url: contextRootPath + "/agent/addInfoInit",
	    			  dataType: "html",
	    			  success: function(data){
	    				  /** 成功构造内容**/
	    				   $("#contentdiv").html(data);
	    				   tinymce.init({
	    					   selector: "textarea#htmEdit",
	    					   theme: "modern",
	    				       language :"zh_CN",
	    				       external_plugins: {
	    							"image": contextRootPath + "/js/tinymce/plugins/image/plugin.min.js"
	    						},
	    				       image_advtab:true
	    				    })
	    				   $("#mceu_13").parent("div").css("width","602px");
	    				   $("#mceu_13").parent("div").css("left","0");
	    				   
	    				   actEvent.addinfoSaveEvent();
	    				   
	    			 }
				});
				
			});
		},
		
        /* 行数据 checkbox 事件bind*/
		lineCheckboxEvent : function (){
			$("#right01 .agen_right_form .agen_right_tab tr:gt(0) input").click(function(){
				//alert($(this).parent('td').parent('tr').index());
				//alert($(this).val());
			});
		},
		/* 行数据 置顶 编辑 删除 事件bind*/
		lineButtonEvent : function (){
			$("#right01 .agen_right_tab tr td:last-child").find('a').click(function(){
				var idx = $(this).index();
				var id = $(this).parent('td').parent('tr').find("td").eq(0).find('input').val();
				if (0 == idx){
					actEvent.setTopAjax(id);
				} else if (1 == idx) {
					actEvent.editInfoInit(id);
				} else if (2 == idx){
					actEvent.delInfoAjax(id);
				}
 				
			});
		},
		/* 行数据 title 事件bind*/
		lineTitleEvent : function (){
			$("#right01 .agen_right_tab tr td:first-child").find('a').click(function(){
			var id = $(this).parent('td').parent('tr').find("td").eq(0).find('input').val();
			$.ajax({
	  			  type: "POST",
	  			  url: contextRootPath + "/agent/clickTitle",
	  			  data:JSON.stringify({"ids": id,"articleTp": 10, "type":"agnav02"}),
	  			  dataType: "html",
	  			  contentType: "application/json; charset=utf-8",
	  			  success: function(data){
	  				  /** 成功构造内容**/
		  				 window.open(contextRootPath + "/agentHomePage/actdetail?id=" + id);
	  			 }
				});
			
			});
		},	
		/* 全选事件 */	
		checkAllEvent : function (){
			$("input[name='checkAll']").click(function(){
				
				if($(this).is(':checked')){
					$("input[name='lineCheck']").prop("checked",true);
				} else {
					$("input[name='lineCheck']").prop("checked",false);
				}
				
			});
		},
		
		/* 底部删除按钮 */
		delEvent : function (){
			$("#right01 #editButton a:eq(0)").click(function(){
				
				var ids = '';
				var i = 0;
				$("#right01 .agen_right_form .agen_right_tab tr:gt(0) input").each(function(){
					if($(this).is(':checked')) {
						ids = ids + "|" + $(this).val();
					} else {
				    	
				    }
				   
				})
				if (ids.length > 0) {
		    		
					actEvent.delInfoAjax(ids);
				} else {
					alert("请选择数据!")
				}

			});
		},
		delInfoAjax:function(ids){
			
			$.ajax({
  			  type: "POST",
  			  url: contextRootPath + "/agent/delInfo",
  			  data:JSON.stringify({"ids": ids,"articleTp": 10, "type":"agnav02"}),
  			  dataType: "html",
  			  contentType: "application/json; charset=utf-8",
  			  success: function(data){
  				  /** 成功构造内容**/
  				   $("#contentdiv").html(data);
  				   actEvent.init();
	    	       alert("删除成功");
  			 }
			});
		},
		
		editInfoInit: function(ids){
			$.ajax({
  			  type: "POST",
  			  url: contextRootPath + "/agent/addInfoInit",
  			  dataType: "html",
  			  success: function(data){
  				  /** 成功构造内容**/
  				   $("#contentdiv").html(data);
  				   tinymce.init({
  					   selector: "textarea#htmEdit",
  					   theme: "modern",
  				       language :"zh_CN",
  				       external_plugins: {
  							"image": contextRootPath + "/js/tinymce/plugins/image/plugin.min.js"
  						},
  				       image_advtab:true
  				    })
  				   $("#mceu_13").parent("div").css("width","602px");
  				   $("#mceu_13").parent("div").css("left","0");
  				   
  					$.ajax({
  		  			  type: "POST",
  		  			  url: contextRootPath + "/agent/editInfoInit",
  		  			  data:JSON.stringify({"ids": ids}),
  		  			  dataType: "json",
  		  			  contentType: "application/json; charset=utf-8",
  		  			  success: function(data){
  		  				  var title = $("#fist input").eq(0).val(data.articleTitle);
  		  				   $.get(data.articleContent ,function(data){
  		  					tinymce.activeEditor.setContent(data); 
  		  	              });
  		  			   }
  					});
  					actEvent.infoEditEvent(ids);
  				   
  			 }
			});
			

		},
		/* 底部置顶按钮 */
		setTopEvent : function (){
			$("#right01 #editButton a:eq(1)").click(function(){

				var ids = '';
				var i = 0;
				$("#right01 .agen_right_form .agen_right_tab tr:gt(0) input").each(function(){
					if($(this).is(':checked')) {
						ids = ids + "|" + $(this).val();
					} else {
				    	
				    }
				})
				if (ids.length > 0) {
		    		
					actEvent.setTopAjax(ids);
				} else {
					alert("请选择数据!")
				}

			
			});
		},
		setTopAjax: function (ids) {
			$.ajax({
  			  type: "POST",
  			  url: contextRootPath + "/agent/setTop",
  			  data:JSON.stringify({"ids": ids,"articleTp": 10, "type":"agnav02"}),
  			  dataType: "html",
  		      contentType: "application/json; charset=utf-8",
  			  success: function(data){
  				  /** 成功构造内容**/
  				   $("#contentdiv").html(data);
  				   actEvent.init();
			   	   alert("置顶成功");
  			 }
			});
			
		},
		/* 底部取消置顶按钮 */
		cancelTopEvent : function (){
			$("#right01 #editButton a:eq(2)").click(function(){

				var ids = '';
				var i = 0;
				$("#right01 .agen_right_form .agen_right_tab tr:gt(0) input").each(function(){
					if($(this).is(':checked')) {
						ids = ids + "|" + $(this).val();
					} else {
				    	
				    }
				   
				})
				if (ids.length > 0) {
		    		$.ajax({
		    			  type: "POST",
		    			  url: contextRootPath + "/agent/cancelTop",
		    			  data:JSON.stringify({"ids": ids,"articleTp": 10, "type":"agnav02"}),
		    			  dataType: "html",
		    			  contentType: "application/json; charset=utf-8",
		    			  success: function(data){
		    				  /** 成功构造内容**/
		    				   $("#contentdiv").html(data);
		    				   actEvent.init();
			    			   alert("取消置顶成功");
		    			 }
					});
					
				} else {
					alert("请选择数据!")
				}

			});
		},
        /* 添加info 保存事件bind*/
		addinfoSaveEvent : function (){
			$("#fist input:eq(1)").click(function(){
				var title = $("#fist input").eq(0).val();
				var content = tinymce.get('htmEdit').getContent();
				if('' == $.trim(title)) {
					alert('请输入标题!');
					return;
				}
				if('' == $.trim(content)) {
					alert('请输入正文!');
					return;
				}
				$.ajax({
	    			  type: "POST",
	    			  url: contextRootPath + "/agent/addInfo",
	    			  data:JSON.stringify({"articleTitle": title, "articleContent": content,"articleTp": 10, "type":"agnav02"}),
	    			  dataType: "html",
	    			  contentType: "application/json; charset=utf-8",
	    			  success: function(data){
	    				  /** 成功构造内容**/
	    				   $("#contentdiv").html(data);
	    				   actEvent.init();
		    			   alert("添加成功");
	    			 }
				});
				
			});
		},
		/* 添加info 修改事件bind*/
		infoEditEvent : function (ids){
			$("#fist input:eq(1)").unbind('click').click(function(){
				var title = $("#fist input").eq(0).val();
				var content = tinymce.get('htmEdit').getContent();
				if('' == $.trim(title)) {
					alert('请输入标题!');
					return;
				}
				if('' == $.trim(content)) {
					alert('请输入正文!');
					return;
				}
				$.ajax({
	    			  type: "POST",
	    			  url: contextRootPath + "/agent/addInfo",
	    			  data:JSON.stringify({"id":ids, "articleTitle": title, "articleContent": content,"articleTp": 10, "type":"agnav02","flag":1}),
	    			  dataType: "html",
	    			  contentType: "application/json; charset=utf-8",
	    			  success: function(data){
	    				  /** 成功构造内容**/
	    				   $("#contentdiv").html(data);
	    				   infoEvent.init();
		    			   alert("编辑成功");
	    			 }
				});
				
			});
		},
		// 排序点击
		sortEvent:function(){
			
			$(".agen_body .agen_right .agen_right_form .agen_right_tab tr:eq(0) td").click(function(){
			    	
			    	var idx = $(this).index();
			    	if(1 == idx || 2 == idx) {
				    	var img = $(this).children("img").attr('src');
				    	if (1 == idx) {
				    		// 创建日期
				    		
				    		if("/web/images/xz.png" == img) {
				    			
				    			actEvent.sortAjax("article_cdt",'ASC');
				    			
				    			
				    		} else if("/web/images/up.png" == img) {
				    			
				    			actEvent.sortAjax("article_cdt",'DESC');
				    			
				    		}
				    		
				    	} else if(2 == idx) {
				    		// 点击量
				    		if("/web/images/xz.png" == img) {
				    			actEvent.sortAjax("brower_cnt",'ASC');
				    			
				    		} else if("/web/images/up.png" == img) {
				    			actEvent.sortAjax("brower_cnt",'DESC');
				    			
				    		}
				    	}
					}
			    	
			    });
			
		},
		sortAjax :function(sortKey,sortOrder){
			
			$.ajax({
  			  type: "POST",
  			  url: contextRootPath + "/agent/sortEvent",
  			  data:JSON.stringify({"articleTp": 10,"orderKey": sortKey,"orderKeySort":sortOrder, "type":"agnav02"}),
  			  dataType: "html",
  			  contentType: "application/json; charset=utf-8",
  			  success: function(data){
  				  /** 成功构造内容**/
  				  $("#contentdiv").html(data);
  				  var idx = 0;
  				  if (sortKey == 'article_cdt') {
  					idx = 1;
  				  } else {
  					idx = 2;
  				  }
  				  if ('ASC' == sortOrder) {
  					$(".agen_body .agen_right .agen_right_form .agen_right_tab tr:eq(0) td").eq(idx).children("img").attr('src', "/web/images/up.png");
  				  } else {
  					$(".agen_body .agen_right .agen_right_form .agen_right_tab tr:eq(0) td").eq(idx).children("img").attr('src', "/web/images/xz.png");
  				  }
  				  
  				  actEvent.init();
  				} 
			});
		}
	    
	   
}
	
});


 